/**
 * Version: 1.0
 * Author: hunny【胡泽雄】
 * Email: huxiong888@163.com
 * Licence: Open Source MIT Licence
 */
(function($){
	$.fn.myallcheckbox = function (options) {
		var defaults = {
			allcheckvalue: ''
		}, s = $.extend({}, defaults, options);
		return this.each(function() {
			var tagName = this.tagName.toLowerCase();
			if ('input' != tagName) {
				return;
			}
			if ('checkbox' != this.type.toLowerCase()) {
				return;
			}
			var $this = $(this);
			$this.bind('click', function(e) {
				checkAll($this);
			});
		});
		function checkAll(t) {
			var name = t.attr('name');
			if (t.val() == s.allcheckvalue) {
				var checked = t.get(0).checked;
				$('input[type="checkbox"][name="' + name + '"][value!=""]').each(function(i) {
					var _this = this;
					var $this = $(this);
					window.setTimeout(function() {
						checked ? _this.checked = true : _this.checked = false;
					}, 0);
				});
				return;
			}
			var length = $('input[type="checkbox"][name="' + name + '"]').length;
			var clength = 0;
			$('input[type="checkbox"][name="' + name + '"][value!=""]').each(function() {
				if (this.checked) {
					clength ++;
				}
			});
			var all = $('input[type="checkbox"][name="' + name + '"][value=""]');
			if (length - 1 == clength) {
				all.get(0).checked = true;
			} else {
				all.get(0).checked = false;
			}
		}
	};
})(jQuery);